package com.stormrage.edu.dao.learn.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.stormrage.edu.dao.learn.LeaStudentVideoDao;
import com.stormrage.entity.learn.rep.LeaStudentVideoRep;
import com.stormrage.entity.learn.view.LeaStudentVideo;
import com.stormragetech.core.dao.MysBaseDao;
import com.stormragetech.core.exception.StormrageTechException;

/**
 * @Title: LeaStudentDaoImpl.java
 * @Package com.stormrage.edu.dao.learn.impl
 * @Description: 学员学习课件 Dao
 * @author www.stormragetech.com
 * @date 2016年4月15日
 * @version V1.0
 */
@Repository
public class LeaStudentVideoDaoImpl extends MysBaseDao implements LeaStudentVideoDao {

	private static final Logger logger = LoggerFactory.getLogger(LeaStudentVideoDaoImpl.class);

	@Override
	public Integer create(final LeaStudentVideo arg0) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 LeaStudentVideoDao 】-【 Create 】- 开始运行 ");

		final String query = "INSERT INTO lea_student_video (stu_id, video_id, time) VALUES"
				+ " (:stuId, :videoId, :time)";
		logger.debug(" 数据库执行语句为 " + query);

		Integer index = create(query, arg0);

		logger.debug(" 【 Dao 】-【 LeaStudentVideoDao 】-【 Create 】- 完成运行 ");
		return index;

	}

	@Override
	public void update(final LeaStudentVideo arg0) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 LeaStudentVideoDao 】-【 Update 】- 开始运行 ");

		final String query = "UPDATE lea_student_video SET time = :time WHERE id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		update(query, arg0);

		logger.debug(" 【 Dao 】-【 LeaStudentVideoDao 】-【 Update 】- 完成运行 ");

	}

	@Override
	public LeaStudentVideo queryById(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 LeaStudentVideoDao 】-【 QueryById 】- 开始运行 ");

		final String query = "SELECT * FROM lea_student_video WHERE id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		LeaStudentVideo entity = queryObject(query, LeaStudentVideo.class, map);

		logger.debug(" 【 Dao 】-【 LeaStudentVideoDao 】-【 QueryById 】- 完成运行 ");
		return entity;

	}

	@Override
	public List<LeaStudentVideo> queryListByStuId(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 LeaStudentVideoDao 】-【 QueryListByStuId 】- 开始运行 ");

		final String query = "SELECT * FROM lea_student_video WHERE stu_id = :stuId";
		logger.debug(" 数据库执行语句为 " + query);

		List<LeaStudentVideo> array = queryList(query, LeaStudentVideo.class, map);

		logger.debug(" 【 Dao 】-【 LeaStudentVideoDao 】-【 QueryListByStuId 】- 完成运行 ");
		return array;

	}

	@Override
	public List<LeaStudentVideoRep> queryListForStudy(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 LeaStudentVideoDao 】-【 QueryListForStudy 】- 开始运行 ");

		final String query = "SELECT g.id, g.time AS len, f.video_len AS tlen FROM edu_major_module AS a,"
				+ " edu_module AS b, edu_module_course AS c, edu_course_chap AS d, edu_chap_video AS e,"
				+ " edu_video AS f, lea_student_video AS g, lea_student AS h, lea_bunch AS i, lea_term AS j"
				+ " WHERE a.module_id = b.id AND b.ness = 0 AND a.module_id = c.module_id"
				+ " AND c.course_id = d.course_id AND d.chap_id = e.chap_id AND e.video_id = f.id"
				+ " AND f.id = g.video_id AND g.stu_id = h.id AND h.bunch_id = i.id AND i.term_id = j.id"
				+ " AND j.major_id = a.major_id AND h.id = :stuId";
		logger.debug(" 数据库执行语句为 " + query);

		List<LeaStudentVideoRep> array = queryList(query, LeaStudentVideoRep.class, map);

		logger.debug(" 【 Dao 】-【 LeaStudentVideoDao 】-【 QueryListForStudy 】- 完成运行 ");
		return array;

	}

	@Override
	public List<LeaStudentVideoRep> queryListForSelectStudy(final Map<String, Object> map)
			throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 LeaStudentVideoDao 】-【 QueryListForStudy 】- 开始运行 ");

		final String query = "SELECT a.id, a.time AS len, e.video_len AS tlen FROM lea_student_video AS a,"
				+ " lea_student_course AS b, edu_course_chap AS c, edu_chap_video AS d, edu_video AS e"
				+ " WHERE a.video_id = d.video_id AND d.chap_id = c.chap_id AND c.course_id = b.course_id"
				+ " AND e.id = a.video_id AND a.stu_id = :stuId AND b.ness = :ness";
		logger.debug(" 数据库执行语句为 " + query);

		List<LeaStudentVideoRep> array = queryList(query, LeaStudentVideoRep.class, map);

		logger.debug(" 【 Dao 】-【 LeaStudentVideoDao 】-【 QueryListForStudy 】- 完成运行 ");
		return array;

	}

	@Override
	public List<LeaStudentVideoRep> queryListForSelectStudyLimit(final Map<String, Object> map)
			throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 LeaStudentVideoDao 】-【 queryListForSelectStudyLimit 】- 开始运行 ");

		final String query = "SELECT a.id, a.time AS len, e.video_len AS tlen FROM lea_student_video AS a,"
				+ " lea_student_course AS b, edu_course_chap AS c, edu_chap_video AS d, edu_video AS e"
				+ " WHERE a.video_id = d.video_id AND d.chap_id = c.chap_id AND c.course_id = b.course_id"
				+ " AND e.id = a.video_id AND a.stu_id = :stuId AND b.ness = :ness LIMIT :startPage, :size";
		logger.debug(" 数据库执行语句为 " + query);

		List<LeaStudentVideoRep> array = queryList(query, LeaStudentVideoRep.class, map);

		logger.debug(" 【 Dao 】-【 LeaStudentVideoDao 】-【 queryListForSelectStudyLimit 】- 完成运行 ");
		return array;

	}

	@Override
	public List<LeaStudentVideoRep> queryRepListByStuCourse(final Map<String, Object> map)
			throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 LeaStudentVideoDao 】-【 queryRepListByStuCourse 】-【 STARTED 】");

		final String query = "SELECT a.id, a.time AS len, e.video_len AS tlen FROM lea_student_video AS a,"
				+ " lea_student_course AS b, edu_course_chap AS c, edu_chap_video AS d, edu_video AS e"
				+ " WHERE a.video_id = d.video_id AND d.chap_id = c.chap_id AND c.course_id = b.course_id"
				+ " AND e.id = a.video_id AND a.stu_id = :stuId AND b.course = :courseId";
		logger.debug(" 数据库执行语句为 " + query);

		List<LeaStudentVideoRep> array = queryList(query, LeaStudentVideoRep.class, map);

		logger.debug(" 【 Dao 】-【 LeaStudentVideoDao 】-【 queryRepListByStuCourse 】-【 COMPLETED 】");
		return array;

	}

}
